package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.ColorAdapter;
import flc.ast.bean.MyColorBean;
import flc.ast.databinding.ActivityPicTextBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class PicTextActivity extends BaseAc<ActivityPicTextBinding> {

    public static String imgPath;
    private ColorAdapter colorAdapter;
    private int oldPosition = 0;


    @Override
    protected int onCreate() {
        return R.layout.activity_pic_text;
    }
    @Override
    protected boolean isStatusBarTextDark() {
        return false;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicTextBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.tvPicTextSave.setOnClickListener(this);
        mDataBinding.ivTextShow.setOnClickListener(this);
        mDataBinding.rvColorList.setLayoutManager(new GridLayoutManager(mContext, 7));
        colorAdapter = new ColorAdapter();
        mDataBinding.rvColorList.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvPicTextSave://保存编辑
                String showText2 = mDataBinding.txtStickerView.getText();
                if (showText2 == null || TextUtils.isEmpty(showText2)) {
                    ToastUtils.showShort(R.string.please_input_text);
                    return;
                }
                mDataBinding.txtStickerView.setShowHelpBox(false);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        saveImg();
                    }
                }, 500);
                break;
            case R.id.ivTextShow://确定
                String showText1 = mDataBinding.etText.getText().toString();
                if (TextUtils.isEmpty(showText1)) {
                    ToastUtils.showShort(R.string.please_input_text);
                    return;
                }
                mDataBinding.txtStickerView.setText(showText1);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        colorAdapter.getItem(oldPosition).setSelect(false);
        colorAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        colorAdapter.getItem(position).setSelect(true);
        colorAdapter.notifyItemChanged(position);
        mDataBinding.txtStickerView.setTextColor(Color.parseColor(colorAdapter.getItem(position).getColor()));
    }

    @Override
    protected void initData() {
        //获取图片
        if (imgPath == null || TextUtils.isEmpty(imgPath)) return;
        Glide.with(this).load(imgPath).into(mDataBinding.imageViewTouch);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.imageViewTouch.setScaleEnabled(false);// 禁用缩放
        setColorData();
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_picture_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlTextSaveView);
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    //准备颜色数据
    private void setColorData() {
        List<MyColorBean> listColor = new ArrayList<>();
        listColor.add(new MyColorBean("#FFFFFF", true));
        listColor.add(new MyColorBean("#000000", false));
        listColor.add(new MyColorBean("#FF7A7A", false));
        listColor.add(new MyColorBean("#FFCA7B", false));
        listColor.add(new MyColorBean("#F7FF84", false));
        listColor.add(new MyColorBean("#95D764", false));
        listColor.add(new MyColorBean("#FFA8A8", false));
        listColor.add(new MyColorBean("#51FFB0", false));
        listColor.add(new MyColorBean("#6DF9FF", false));
        listColor.add(new MyColorBean("#446AFF", false));
        listColor.add(new MyColorBean("#A362FF", false));
        listColor.add(new MyColorBean("#E661FF", false));
        listColor.add(new MyColorBean("#00A27F", false));
        listColor.add(new MyColorBean("#FF5555", false));
        colorAdapter.setList(listColor);
        mDataBinding.txtStickerView.setTextColor(Color.parseColor("#FFFFFF"));
    }

}